

%% load panel data
load('data_panel.mat')


%% aggregate data for entire period, 1950-1999

countries=unique(data_panel.cown);

data=data_panel(1:length(countries),[1:3,6:end]);
for n=1:length(countries)
    dn=data_panel(data_panel.cown==countries(n),:);
    data(n,1:3)=dn(1,1:3);
    data(n,4:end)=array2table(nanmean(table2array(dn(:,6:end))));
end

outcomes=outcomes_panel(1:length(countries),[1:3,5:end]);
for n=1:length(countries)
    dn=outcomes_panel(outcomes_panel.cown==countries(n),:);
    outcomes(n,1:3)=dn(1,1:3);
    outcomes(n,4:end)=array2table(1*(sum(table2array(dn(:,5:end)))>0));
end

missing=find(~isfinite(data.gdp_pc)|~isfinite(data.pop)...
    |~isfinite(data.polity)|~isfinite(data.USally));
data(missing,:)=[];
outcomes(missing,:)=[];


%%
clearvars -except data outcomes
save('data.mat')

